/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.hitemole.screen;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.hitemole.game.GameCore;

public class ResourceScreen implements AssetErrorListener,Screen{
        SpriteBatch batch;
        boolean loading = false;
		private GameCore core;
		private Screen screen;
        public ResourceScreen(GameCore core) {
			// TODO Auto-generated constructor stub
        	this.core=core;
        	this.screen=null;
		}

		private void load () {
            core.getResourceManager().getAssetManager().load("Textures.atlas", TextureAtlas.class);
            core.getResourceManager().getAssetManager().load("Textures.json", Skin.class);
		}

        @SuppressWarnings("rawtypes")
		@Override
        public void error (AssetDescriptor asset, Throwable throwable) {
                Gdx.app.error("AssetManagerTest", "Couldn't load asset: " + asset, (Exception)throwable);
        }

        @Override
        public void dispose () {
        	batch.dispose();
        }

		@Override
		public void render(float delta) {
			// TODO Auto-generated method stub
            Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

            core.getResourceManager().getAssetManager().update();      
            batch.begin();
            loading = core.getResourceManager().getAssetManager().isLoaded("Textures.atlas")&
            		core.getResourceManager().getAssetManager().isLoaded("Textures.json");
//            System.out.println();
            if(loading){
            	System.out.println("Loading complete");
            	core.getResourceManager().createInstance();
            	if(screen==null){
            		core.setScreen(core.getMenuScreen());
            	}else{
            		core.setScreen(screen);
            	}
            }
            batch.end();

		}

		@Override
		public void resize(int width, int height) {
			// TODO Auto-generated method stub
			
		}
		
		public void setScreen(Screen screen){
			this.screen=screen;
		}

		@Override
		public void show() {
			// TODO Auto-generated method stub
			  Gdx.app.setLogLevel(Application.LOG_ERROR);

//              Resolution[] resolutions = {new Resolution(320, 480, ".320480"), new Resolution(480, 800, ".480800"),
//                      new Resolution(480, 856, ".480854")};
//              ResolutionFileResolver resolver = new ResolutionFileResolver(new InternalFileHandleResolver(), resolutions);
//              manager = new AssetManager();
//              manager.setLoader(Texture.class, new TextureLoader(resolver));
              core.getResourceManager().getAssetManager().setErrorListener(this);
              load();
              Texture.setAssetManager(core.getResourceManager().getAssetManager());
              batch = new SpriteBatch();
		}

		@Override
		public void hide() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void pause() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void resume() {
			// TODO Auto-generated method stub
			
		}
}